package gemini.pollux.ui.client.page.content.marketingplan.level.list;

import gemini.basic.model.Level;
import gemini.pollux.ui.client.ServiceCatalog;
import gemini.pollux.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.pollux.ui.client.mvp.single.PolluxWidgetDisplay;
import gemini.pollux.ui.client.mvp.single.PolluxWidgetPresenter;
import gemini.pollux.ui.client.page.RootEvent;
import gemini.pollux.ui.client.page.content.marketingplan.level.LevelEvent;
import gemini.pollux.ui.client.page.content.marketingplan.level.LevelObject;
import gemini.pollux.ui.client.page.content.marketingplan.level.widget.LevelTableEvent;
import gemini.pollux.ui.client.page.content.marketingplan.level.widget.LevelTableHandler;
import gemini.pollux.ui.client.page.content.marketingplan.level.widget.LevelTableWidget;
import gemini.pollux.ui.client.utils.GuiUtils;
import gemini.pollux.ui.client.utils.UIConstants;

import java.util.List;

import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.inject.Inject;

public class LevelListPresenter extends
		PolluxWidgetPresenter<LevelListPresenter.Display> {

	@Inject
	public LevelListPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends PolluxWidgetDisplay {
		void showLevels(List<Level> levels);

		LevelTableWidget getLevelTableWidget();
	}

	public static final Place PLACE = new Place(UIConstants.PLACE_LEVEL);
	private Level outputObject;
	
	
	@Override
	protected void onBind() {
		registerHandler(display.getLevelTableWidget().addLevelActionHandler(new LevelTableHandler() {
			
			@Override
			public void onEdit(LevelTableEvent event) {
				outputObject = event.getLevel();
				localEventBus.fireEvent(new LevelEvent(LevelEvent.ForwardType.EDIT));
			}
			
			@Override
			public void onDelete(LevelTableEvent event) {
				eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
				ServiceCatalog.getLevelService().deleteLevel(event.getLevel().getId(), 
						new AbstractAsyncCallback<Void>() {

							@Override
							public void onSuccess(Void result) {
								localEventBus.fireEvent(new LevelEvent(LevelEvent.ForwardType.BACK_TO_LIST));
							}
							
							@Override
							public void onFailure(final Throwable caught) {
								GuiUtils.alert("This Level is in used! Cannot delete!");
							}
						});
			}
			
			@Override
			public void onAdd(LevelTableEvent event) {
				localEventBus.fireEvent(new LevelEvent(LevelEvent.ForwardType.NEW));
			}
		}));
		
		display.initCustom();
		display.showLevels(levels);
	}
	
	private List<Level> levels;
	
	@Override
	public void onUnbind(){
		levels = null;
	}
	
	@Override
	public void setInputObject(Object ob){
		LevelObject levelOb = (LevelObject) ob;
		if (levelOb != null) {
			levels = levelOb.getLevels();
		}
	}


	@Override
	public Place getPlace() {
		return PLACE;
	}
	
	@Override
	public Object getOutputObject(){
		return this.outputObject;
	}
}
